package com.example.paper.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.paper.R;

public class BottomButton extends RelativeLayout implements View.OnClickListener{

    private int normalImg = 0;//默认未选中图片id
    private int selImg = 0;//选中图片id
    private int normaColor = 0;//默认为选中文字颜色
    private int selColor = 0;//选中图片颜色
    private String content;//设置底部显示文字
    private boolean showPoint = false;//是否显示小红点标志
    private boolean selFlag = false;//当前控件是否选中
    //布局添加
    private LinearLayout linearLayout;
    private ImageView img;
    private TextView contentTv;
    private TextView numTv;
    private SelectListener listener;

    public BottomButton setListener(SelectListener listener) {
        this.listener = listener;

        return this;
    }

    public BottomButton setContent(String content) {
        this.content = content;
        //检测是否为空
        if (this.content.isEmpty())
            return this;
        //设置内容
        contentTv.setText(content);
        return this;
    }

    public BottomButton setNormalImg(int normalImg) {
        this.normalImg = normalImg;
        if (!selFlag)
            img.setImageResource(this.normalImg);
        return this;
    }

    public BottomButton setSelImg(int selImg) {
        this.selImg = selImg;
        if (selFlag)
            img.setImageResource(this.selImg);
        return this;
    }
    //未选中
    public BottomButton setNormaColor(int normaColor) {
        this.normaColor = normaColor;
        if (!selFlag)
            contentTv.setTextColor(this.normaColor);
        return this;
    }
    //选中
    public BottomButton setSelColor(int selColor) {
        this.selColor = selColor;
        if (selFlag)
            contentTv.setTextColor(this.selColor);
        return this;
    }

    public BottomButton setShowPoint(boolean showPoint) {
        this.showPoint = showPoint;
        if (this.showPoint)
            numTv.setVisibility(VISIBLE);
        else
            numTv.setVisibility(GONE);
        return this;
    }


    //可以获取控件属性
    public BottomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.bottom_arras);
        selFlag = typedArray.getBoolean(R.styleable.bottom_arras_selflag,false);
        typedArray.recycle();//释放TypedArray对象防止内存泄露
        //添加布局
        init();
    }

    private void init(){
        //添加布局方法
        linearLayout = new LinearLayout(getContext());
        img = new ImageView(getContext());
        contentTv = new TextView(getContext());
        numTv = new TextView(getContext());
        //组合布局拼凑一起
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        //包裹
        LinearLayout.LayoutParams imgLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(imgLp);
        LinearLayout.LayoutParams contentLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        contentTv.setLayoutParams(contentLp);
        //垂直请求居中
        linearLayout.setGravity(Gravity.CENTER);
        //添加
        linearLayout.addView(img);
        linearLayout.addView(contentTv);
        //设置RelativeLayout宽高
        RelativeLayout.LayoutParams linearLp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        linearLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        linearLayout.setLayoutParams(linearLp);
        addView(linearLayout);
        //添加TextView
        RelativeLayout.LayoutParams numLp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //边距
        numLp.rightMargin = ConvertUtils.dp2px(15);
        //去右边
        numLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        numTv.setLayoutParams(numLp);
        addView(numTv);
        setOnClickListener(this::onClick);
    }

    //点击事件
    @Override
    public void onClick(View v) {
        selFlag = true;
        refreshSelect();
        if (listener!=null)
            listener.onSelect(getId());
    }

    public void clearSelect(){
        selFlag = false;
        img.setImageResource(normalImg);
        contentTv.setTextColor(normaColor);
    }

    //刷新方法
    private void refreshSelect(){
        if (selFlag){
            img.setImageResource(selImg);
            contentTv.setTextColor(selColor);
            numTv.setVisibility(GONE);
        }
    }

    public interface SelectListener{
        void onSelect(int id);
    }

}
